/**
 * 列表页公共属性及方法
 */
export default {
    data() {
        return {
            search: {},
            deleteApi: "",
            exportApi: "",
            exportLoading: false,
            filterConfig: [],
            extendFormFilter: {}
        }
    },
    created() {},
    methods: {
        resetFormFilter(data) {
            for (const key in this.extendFormFilter) {
                this.extendFormFilter[key] = ""
            }
            data = Object.assign({}, data, this.extendFormFilter)
            this.render(data)
        },
        //监听筛选提交
        listenFilter(data) {
            data = Object.assign({}, data, this.extendFormFilter)
            this.$refs.table.filterValue.page = 1
            this.$refs.table.filterValue.limit = 10
            this.render(data)
        },
        render(data = {}) {
            this.$refs.table.filterValue = Object.assign(this.$refs.table.filterValue, data)
            this.handleRenderTable();
        },
        /**
         * 设置表格筛选参数
         * @param key   键
         * @param value 值
         */
        handleSetFilter(key, value) {
            this.$refs.table.filterValue[key] = value
        },

        /**
         * 获取表格筛选参数
         * @param key   键
         * @returns {*}
         */
        handleGetFilter(key) {
            return this.$refs.table.filterValue[key]
        },

        /**
         * 重新渲染表格
         */
        handleRenderTable(refNmae = 'table') {
            this.$refs[refNmae].renderTable();
        },

        /**
         * 移除表格中某行数据
         * @param index 下标
         */
        handleDeleteRow(index) {
            this.$refs.table.deleteRow(index);
        },

        /**
         * 删除数据
         * @param callback  删除操作
         * @param index     下标
         */
        delete({
            row,
            index
        }) {
            this.loading = true
            this.deleteApi(row.ID).then((res) => {
                if (res.data.code === 0) {
                    this.$message.success('删除成功');
                    this.handleDeleteRow(index);
                } else {
                    this.$message.error(res.data.msg);
                }
                this.loading = false;
            }).catch((error) => {
                this.loading = false;
                this.$message.error('删除失败');
            });
        },
        /**
         * @param 导出excel
         */
        exportExcel() {
            if (this.$refs.table.dataInfo.length === 0) {
                this.$message.error('表格无数据，不能导出excel文件！')
                return
            }
            if('limit' in this.$refs.table.filterValue){
                this.$refs.table.filterValue.limit = this.$refs.table.total
            }
            let data = Object.assign({}, this.$refs.table.filterValue, this.extendFormFilter)
            this.exportLoading = true
            this.exportApi(data).then(response => {
                this.downloadBlob(response)
                this.exportLoading = false
            }).catch(() => {
                this.exportLoading = false
            })
        },
        /**
         * 设置子集数据，treeTable
         * @param row
         * @param children
         */
        handleSetChild(data) {
            return new Promise((resolve, reject) => {
                data.forEach(item => {
                    if (item.children_count > 0) {
                        item.hasChildren = true
                    }
                })
                resolve(data)
            })
        },

        /**
         * 开启表格loading
         */
        handleOpenLoding() {
            this.$refs.table.loading = true;
        },

        /**
         * 关闭表格loading
         */
        handleCloseLoding() {
            this.$refs.table.loading = false;
        },
        /**
         * 处理列表下级数据
         * @param {*} data
         * @return {*} 
         */
        renderTableLoad(data) {
            return new Promise((resolve, reject) => {
                resolve(data.children)
            })
        },
    }
}